<template>
  <div class="container">
    <h2 class="handle-title">系统管理 > 修改教师</h2>
    <el-form ref="form" :rules="rules" :model="nowTeacher" class="demo-ruleForm" label-width="80px" style="width:430px">
      <el-form-item label="教师名字" prop="usertruename">
        <el-input v-model="nowTeacher.usertruename" style="width:190px" maxlength="20"></el-input>
      </el-form-item>
<!--      <el-form-item label="角色状态">-->
<!--        <el-select v-model="form.rolestate" placeholder="请选择角色状态">-->
<!--          <el-option label="可用" value="1"></el-option>-->
<!--          <el-option label="不可用" value="0"></el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="职称">
        <el-input  v-model="nowTeacher.tecttitle"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onUpdate('form')">修改</el-button>
        <el-button @click="goBack">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {selectTeacherById} from "../../api/public/api";
import {updateTeacher} from "../../api/public/api";

export default {
  name: '',
  data() {
    return {
      rules: {
        rolename: [
          {required: true, message: '请输入角色名称', trigger: 'blur'}
        ]
      },
      nowTeacher: {
        usertruename:'',
        tecid: '',
        userid:'',
        tecttitle:'',
        userphone:''
      }
    }
  },
  mounted() {
    let that = this;
    //获取修改的数据id
    let id = this.$route.query.id
    //根据ID查询教师的详细信息
    selectTeacherById(id).then((res) => {
      this.nowTeacher.usertruename = res.data.data.usertruename;
      this.nowTeacher.tecid = res.data.data.tecid;
      this.nowTeacher.userid = res.data.data.userid;
      this.nowTeacher.tecttitle = res.data.data.tectitle;
      this.nowTeacher.userphone = res.data.data.userphone;
    })
  },
  methods: {
    //修改角色
    onUpdate(form) {
      let that = this;
      this.$refs[form].validate((value) => {
        // console.log(value)
        if (value) {
          let formData = new FormData();
          formData.append("usertruename", that.nowTeacher.usertruename);
          formData.append("tectitle", that.nowTeacher.tecttitle);
          formData.append("userid", that.nowTeacher.userid);
          formData.append("tecid", that.nowTeacher.tecid);
          console.log(formData);
          updateTeacher(formData).then((res) => {
            if (res.data.code == 1) {
              that.$message({
                message: "修改成功",
                type: "success",
              })
              that.$router.push("/teacher")
            } else {
              this.$message({
                message: "修改失败",
                type: "info"
              });
            }
          })
        }
      })
    },
    //取消操作
    goBack() {
      this.$router.go(-1);
    },
  },
}
</script>

<style scoped>

</style>
